package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
public class LoginController {
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/login")
    @ResponseBody
    public JsonResult login(String username,String password){
        Employee employee = employeeService.getByUsernameAndPassword(username, password);
        if(employee == null){
            return new JsonResult("账户不存在",false);
        }
        if(!employee.isAdmin()){
            List<String> permissionExpression = employeeService.selectByEmployeeId(employee.getId());
            UserContext.setExpression(permissionExpression);
        }
        UserContext.setEmployee(employee);
        return new JsonResult();
    }

    @RequestMapping("/logout")
    public String logout(){
        UserContext.removeSession();
        return "redirect:/login.html";
    }
}
